package com.ebelter.seneo.ui.activitys;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.adapters.MembersAdapter;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoDbUtils;
import com.ebelter.seneo.ui.views.AddNameDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class GuestSelectActivity extends SeneoBaseActivity{


    public static final int REQUEST_CODE = 87;
    public static final int RESULT_CODE = 88;
    public static final String TAG = "GuestSelectActivity";
    public static final String SELECT_USERID = "1";

    ImageView su_black_iv;
    ListView guestSelect_lv;
    MembersAdapter membersAdapter;
    @Override
    protected int loadLayoutById() {
        return R.layout.activity_guestselect_ly;
    }

    @Override
    protected void initView() {
        super.initView();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        FV();
    }

    private void FV() {
        su_black_iv=findViewById(R.id.su_black_iv);
        guestSelect_lv=findViewById(R.id.guestSelect_lv);

        su_black_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        membersAdapter=new MembersAdapter(this);
        guestSelect_lv.setAdapter(membersAdapter);


        findViewById(R.id.view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //添加底部按钮
        View footView = View.inflate(this,R.layout.item_members_guestselect_ly,null);
        ImageView iv1 = footView.findViewById(R.id.item_mem_iv1);
        TextView tv1 = footView.findViewById(R.id.item_mem_tv1);
        tv1.setText("Add");
        iv1.setImageResource(R.drawable.ic_white_add);

        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addUser();
            }
        });

        guestSelect_lv.addFooterView(footView);

        guestSelect_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent();
                long selectId = membersAdapter.getItemId(position);
                intent.putExtra(SELECT_USERID,selectId);
                setResult(RESULT_CODE,intent);
                finish();


            }
        });
    }

    private void addUser() {
        LogUtils.i(TAG2,"---addUser");

        AddNameDialog addNameDialog=new AddNameDialog(this);
        addNameDialog.show();
        addNameDialog.setListener(new AddNameDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                LogUtils.i(TAG2,"----关闭");
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView, String str) {
                LogUtils.i(TAG2,"----确定 str = " + str);
                //1 先验证昵称不可以为空和先查询一遍数据库有没有这个名字

                if (TextUtils.isEmpty(str)){
                    ToastUtil.show(R.string.Nikename_cannot_be_empty);
                    return;
                }
                if (str.length()>20){
                    ToastUtil.show(R.string.nikename_over_tip);
                    return;
                }
                if (membersAdapter!=null){
                    boolean isContain = membersAdapter.isContain(str);
                    if (isContain){
                        ToastUtil.show("Nickname already exists.");//昵称已经存在
                        return;
                    }
                    long hostId = SeneoDbUtils.getHostId();
                    SeneoAddInfoActivity.openSeneoAddInfoActivity(GuestSelectActivity.this,1001,SeneoAddInfoActivity.CMD_register_children,str,hostId);
                    dialogView.cancel();
                }



            }
        });



    }

    @Override
    protected void initData() {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.i(TAG2,"onActivityResult---requestCode="+requestCode+"---resultCode="+resultCode);
        if (requestCode==1001){
            if (resultCode==RESULT_OK){
                LogUtils.i(TAG2,"onActivityResult---说明添加用户成功回来");
                if (membersAdapter!=null)membersAdapter.notifyDataSetChanged2();
                long userid = data.getLongExtra("SELECT_USERID",0);

                Intent intent = new Intent();
                long selectId = userid;
                intent.putExtra(SELECT_USERID,selectId);
                setResult(RESULT_CODE,intent);
                finish();
            }else {
                LogUtils.i(TAG2,"onActivityResult---说明添加用户失败回来");
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, TAG+"--onEventMainThread接收到CommonEventBus信息--" + event.toString());
        if (event.cmd== IUserContant.ICMDS.ADD_ONE_CHILDREN_USER_SUCCESS){
            LogUtils.i(TAG, TAG+"--onEventMainThread添加一个子用户成功");
            if (membersAdapter!=null)membersAdapter.notifyDataSetChanged2();
        }
    }
}
